package com.enums;

import org.junit.Test;

import java.lang.reflect.Method;

/**
 * @classDesc: 功能描述:
 * @author: 李明杰
 * @date: 2021/11/13 2:40
 * @copyright 众阳健康
 */
public class EnumTest {
    @Test
    public void annotation() throws Exception {
//        Class<?> aClass = Class.forName("com.enums.UserTypeEnum");

    }

    @Test
    public void reflect() throws Exception {
        Class<?> aClass = Class.forName("com.enums.UserTypeEnum");
//        Method getMeaning = aClass.getDeclaredMethod("getMeaning", String.class);
        // 错误的方式，枚举对应的class没有newInstance方法，会报NoSuchMethodException，应该使用getEnumConstants方法
        //Object o = aClass.newInstance();NoSuchMethodException
//        Object[] oo = aClass.getEnumConstants();
//        Object invoke = getMeaning.invoke(oo[0],"1");

        String meaning = "boss";
        /**获取value*/
        Method getValue = aClass.getDeclaredMethod("getValue");
        /**获取meaning*/
        Method getMeaning = aClass.getDeclaredMethod("getMeaning");
        Object[] enumConstants = aClass.getEnumConstants();//返回枚举元素
        for (int i = 0; i < enumConstants.length; i++) {
            if (meaning.equals(getMeaning.invoke(enumConstants[i])))
                System.err.println(getValue.invoke(enumConstants[i]).toString());

        }
    }
}
